//
//  MTJoinTaskCell.swift
//  MT
//
//  Created by ooops on 2020/5/23.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTJoinTaskCell: UITableViewCell {
    
    class func reuseIdentity() -> String {
        return "MTJoinTaskCellID"
    }
    
    var model: MTPartyTaskListInfoModel? {
        didSet {
            partyImgView.mtSetImage(model?.href, nil)
            if let m = model {
                bindData(model: m)
            }
            partyTypeNameLabel.text = model?.typeName
            partyNameLabel.text = model?.name
            if model?.certification == true {
                partyTypeLabel.text = "商"
                partyTypeLabel.isHidden = false
                partyTypeNameLabel.snp.remakeConstraints { (make) in
                    make.centerY.equalTo(partyTypeLabel)
                    make.height.equalTo(14)
                    make.left.equalTo(partyTypeLabel.snp.right).offset(5)
                }
            } else {
                partyTypeLabel.isHidden = true
                partyTypeNameLabel.snp.remakeConstraints { (make) in
                    make.centerY.equalTo(partyTypeLabel)
                    make.height.equalTo(14)
                    make.left.equalTo(partyNameLabel.snp.right).offset(5)
                }
            }
            
            if let dateString = model?.startTime,
                let date = DateManager.date(dateString: dateString, format: "yyyy-MM-dd HH:mm") {
                //dateLabel.attributedText = arrtibute(text: DateManager.string(date: date, format: "MM-dd HH:mm"), icon: "icon_time", position: 0)
                timeLabel.text = DateManager.string(date: date, format: "MM-dd HH:mm")
            }
            //addressLabel.attributedText = arrtibute(text: "\(model?.address ?? "")", icon: "icon_location", position: 0)
            addressLabel.text = "\(model?.address ?? "")"
            descriptionLabel.text = model?.remark
            
            //giftImgView.mtSetImage(model?.giftHref, nil)
            if let num = model?.giftNum {
                giftAmountLabel.attributedText = arrtibute(text: "\(num)")
            }
        }
    }
    
    private func bindData(model: MTPartyTaskListInfoModel) {
        converView.isHidden = true
        let invite = model.inviteNum ?? 0
        let append = invite > 0 && (model.status == .accept || model.status == .ended || model.status == .started)
        let inviteAppendString = (append ? "(限\(invite)人)" : "")
        switch model.status {
        case .wating:
            statusLabel.text = "招募中"
            statusLabel.textColor = UIColor(hex: 0xffc000)
            statusLabel.backgroundColor = UIColor.text.x1d1a1a
        case .invite:
            statusLabel.text = "审核中"
            statusLabel.textColor = UIColor.text.x1d1a1a
            statusLabel.backgroundColor = UIColor(hex: 0xffc000)
        case .ended:
            statusLabel.text = "已结束" + inviteAppendString
            statusLabel.textColor = UIColor(hex: 0xffffff)
            statusLabel.backgroundColor = UIColor.text.x9b9b9b
            converView.isHidden = false
        case .accept:
            /// 是自己发布的任务
            if model.userType == 1 {
                statusLabel.text = "招募中" + inviteAppendString
                statusLabel.textColor = UIColor(hex: 0xffc000)
                statusLabel.backgroundColor = UIColor.text.x1d1a1a
            } else {
                statusLabel.text = "已通过" + inviteAppendString
                statusLabel.textColor = UIColor.text.x1d1a1a
                statusLabel.backgroundColor = UIColor(hex: 0x36e521)
            }
        case .reject:
            statusLabel.text = "已拒绝"
            statusLabel.textColor = UIColor(hex: 0xffffff)
            statusLabel.backgroundColor = UIColor.text.x9b9b9b
        case .started:
            statusLabel.text = "进行中" + inviteAppendString
            statusLabel.textColor = UIColor.text.x1d1a1a
            statusLabel.backgroundColor = UIColor(hex: 0xffc000)
        default:
            break
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        makeConstraint()
        selectionStyle = .none
    }
    
    lazy var partyImgView: UIImageView = {
        let imageV = UIImageView()
        imageV.contentMode = .scaleAspectFill
        imageV.layer.masksToBounds = true
        return imageV
    }()
    
    lazy var converView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xd8d8d8, alpha: 0.6)
        v.isHidden = true
        return v
    }()
    
    lazy var statusLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(12)
        l.textColor = UIColor.text.x1d1a1a
        l.textAlignment = .center
        l.text = "报名成功"
        return l
    }()
    
    lazy var partyNameLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(16)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "任务名称"
        return l
    }()
    
    lazy var partyTypeLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(12)
        l.textColor = UIColor.white
        l.text = "下"
        l.backgroundColor = UIColor.blue
        l.textAlignment = .center
        return l
    }()
    
    lazy var partyTypeNameLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(12)
        l.textColor = UIColor.white
        l.text = "下午茶"
        l.backgroundColor = UIColor(hex: 0xffc000)
        return l
    }()
    
    lazy var dateLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x1d1a1a
        return l
    }()
    
    lazy var timeImgView: UIImageView = {
        let imageV = UIImageView()
        imageV.contentMode = .scaleAspectFill
        imageV.image = UIImage(named: "icon_time")
        return imageV
    }()
    
    lazy var timeLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x1d1a1a
        l.numberOfLines = 2
        l.preferredMaxLayoutWidth = ScreenWidth - 160 - 48
        return l
    }()
    
    lazy var addressImgView: UIImageView = {
        let imageV = UIImageView()
        imageV.contentMode = .scaleAspectFill
        imageV.image = UIImage(named: "icon_location")
        return imageV
    }()
    
    lazy var addressLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x1d1a1a
        l.numberOfLines = 2
        l.lineBreakMode = .byCharWrapping
        l.preferredMaxLayoutWidth = ScreenWidth - 160 - 48
        return l
    }()
    
    lazy var descriptionLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x9b9b9b
        return l
    }()
    
    lazy var giftImgView: UIImageView = {
        let imageV = UIImageView()
        imageV.contentMode = .scaleAspectFit
        imageV.clipsToBounds = true
        imageV.image = UIImage(named: "club_gift")
        return imageV
    }()
    
    lazy var giftAmountLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(24)
        l.textColor = UIColor.text.x1d1a1a
        return l
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.seprateLine.x707070
        return v
    }()
    
    lazy var bottomLineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xf4f4f4)
        return v
    }()
    
    private func setupUI() {
        contentView.addSubview(partyImgView)
        partyImgView.addSubview(converView)
        partyImgView.addSubview(statusLabel)
        contentView.addSubview(partyNameLabel)
        contentView.addSubview(partyTypeLabel)
        contentView.addSubview(partyTypeNameLabel)
        
        contentView.addSubview(timeImgView)
        contentView.addSubview(timeLabel)
        contentView.addSubview(addressImgView)
        contentView.addSubview(addressLabel)
        contentView.addSubview(descriptionLabel)
        
        contentView.addSubview(giftImgView)
        contentView.addSubview(giftAmountLabel)
        contentView.addSubview(bottomLineView)
        contentView.addSubview(lineView)
    }
    
    private func makeConstraint() {
        lineView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(bottomLineView.snp.top)
            make.height.equalTo(1)
        }
        
        bottomLineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(5)
        }
        
        converView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        partyImgView.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview()
            make.width.equalTo(160)
            make.height.equalTo(160)
        }
        
        statusLabel.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(bottomLineView.snp.top)
            make.height.equalTo(20)
        }
        
        partyNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(partyImgView).offset(10)
            make.left.equalTo(partyImgView.snp.right).offset(10)
            make.width.lessThanOrEqualTo(150)
        }
        
        partyTypeLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(partyNameLabel)
            make.left.equalTo(partyNameLabel.snp.right).offset(5)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        partyTypeNameLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(partyTypeLabel)
            make.left.equalTo(partyTypeLabel.snp.right).offset(5)
            make.height.equalTo(14)
        }
        
        timeImgView.snp.makeConstraints { (make) in
            make.left.equalTo(partyNameLabel)
            make.top.equalTo(partyNameLabel.snp.bottom).offset(6)
            make.size.equalTo(CGSize(width: 15, height: 15))
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(timeImgView.snp.right).offset(2)
            //make.top.equalTo(timeImgView).offset(-2)
            make.top.equalTo(timeImgView)
            make.right.equalToSuperview().offset(-10)
        }
        
        addressImgView.snp.makeConstraints { (make) in
            make.left.equalTo(timeImgView)
            make.top.equalTo(timeImgView.snp.bottom).offset(6)
            make.size.equalTo(CGSize(width: 15, height: 15))
        }
        
        addressLabel.snp.makeConstraints { (make) in
            make.left.equalTo(timeLabel)
            //make.top.equalTo(addressImgView).offset(-2)
            make.top.equalTo(addressImgView)
            make.right.equalTo(timeLabel)
        }
        
        descriptionLabel.snp.makeConstraints { (make) in
            make.left.equalTo(addressLabel)
            make.top.equalTo(addressLabel.snp.bottom)
            make.right.equalToSuperview().offset(-10)
        }
        
        giftImgView.snp.makeConstraints { (make) in
            make.left.equalTo(descriptionLabel)
            //make.top.equalTo(descriptionLabel.snp.bottom).offset(6)
            make.bottom.equalTo(bottomLineView.snp.top).offset(-5)
            make.width.equalTo(42)
            make.height.equalTo(29)
        }
        
        giftAmountLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(giftImgView).offset(5)
            make.left.equalTo(giftImgView.snp.right).offset(10)
        }
    }
    
    private func arrtibute(text: String) -> NSAttributedString {
        let markAttribute = NSMutableAttributedString(string: text)
        let arrtibuteValue: [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor : UIColor.text.x1d1a1a,
                                                             NSAttributedString.Key.font : PingFangSCSemiboldFont(24)]
        
        markAttribute.addAttributes(arrtibuteValue, range: NSRange(location: 0, length: text.count))
        
        let xAttribute = NSMutableAttributedString(string: "x ")
        let xarrtibuteValue: [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor : UIColor.text.x1d1a1a,
                                                               NSAttributedString.Key.font : PingFangSCSemiboldFont(18)]
        xAttribute.addAttributes(xarrtibuteValue, range: NSRange(location: 0, length: "x ".count))
        
        markAttribute.insert(xAttribute, at: 0)
        
        return markAttribute
    
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
